package org.ztr.yanai.ai.config;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @Author: ZhaoTR
 * @Date: Created in 2025/10/30 20:21
 * @Description: 配置 DashScope 多模型支持（Qwen + DeepSeek），并集成 ToolCallbacks
 * @Version: 1.0
 */
@Configuration
public class SaaLLMConfig {

    private static final Logger logger = LoggerFactory.getLogger(SaaLLMConfig.class);

    private static final String DEEPSEEK_MODEL = "deepseek-v3";
    private static final String QWEN_MODEL = "qwen-max";

    /**
     * 全局 DashScope API 实例（可复用）
     */
    @Bean
    public DashScopeApi dashScopeApi() {
        return DashScopeApi.builder()
                .apiKey(System.getenv("aiQwen-apikey"))
                .build();
    }

    /**
     * DeepSeek 模型 Bean
     */
    @Bean("deepseek")
    public ChatModel deepseekChatModel(DashScopeApi dashScopeApi) {
        return DashScopeChatModel.builder()
                .dashScopeApi(dashScopeApi)
                .defaultOptions(DashScopeChatOptions.builder().withModel(DEEPSEEK_MODEL).build())
                .build();
    }

    /**
     * Qwen 模型 Bean（设为 @Primary，作为默认模型）
     */
    @Bean("qwen")
    @Primary
    public ChatModel qwenChatModel(DashScopeApi dashScopeApi) {
        return DashScopeChatModel.builder()
                .dashScopeApi(dashScopeApi)
                .defaultOptions(DashScopeChatOptions.builder().withModel(QWEN_MODEL).build())
                .build();
    }

    /**
     * DeepSeek 专用 ChatClient（不带工具回调）
     */
    @Bean("deepseekChatClient")
    public ChatClient deepseekChatClient(@Qualifier("deepseek") ChatModel model) {
        return ChatClient.builder(model)
                .defaultOptions(ChatOptions.builder().model(DEEPSEEK_MODEL).build())
                .build();
    }

    /**
     * Qwen 专用 ChatClient（带工具回调，作为默认客户端）
     */
    @Bean("qwenChatClient")
    @Primary
    public ChatClient qwenChatClient(
            @Qualifier("qwen") ChatModel chatModel,
            ToolCallbackProvider tools) {
        logger.info("ChatModel 类型: {}", chatModel.getClass().getName());
        logger.info("通义千问注册的工具数量: {}", tools.getToolCallbacks().length);
        return ChatClient.builder(chatModel)
                .defaultOptions(ChatOptions.builder().model(QWEN_MODEL).build())
                .defaultToolCallbacks(tools.getToolCallbacks())
                .build();
    }

}